//
//  PictureListToAdd.swift
//  Fancyswap
//
//  Created by Jonathan Muschalle on 2015-03-07.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class PictureListToAdd {
    
    let INDEX_NOT_FOUND:Int = -1
    let PROGRESS_DONE:Float = 1
    private var pictures = [PictureToAdd]()
    private var uploadFormId:NSTimeInterval = 0
 
    init() {
        pictures = [PictureToAdd]()
        initUploadFormIdWithUniqueName()
    }
    
    
    func clear() {
        pictures.removeAll(keepCapacity: true)
        initUploadFormIdWithUniqueName()
    }
    
    func initUploadFormIdWithUniqueName() {
        let date = NSDate()
        uploadFormId = date.timeIntervalSince1970
    }
    
    func getUploadFormId() -> NSTimeInterval {
        return uploadFormId
    }
    
    func addPictureFromPath(picturePath:String) {
        let pic = Picture.createPictureFromUrl(picturePath)
        pictures.append(pic)
    }
    
    func addPictureFromImage(picture:UIImage) {
        let pic = Picture.createPictureFromImageObject(picture)
        pictures.append(pic)
    }
    
    func getPictureById(index:Int) -> PictureToAdd {
        return pictures[index]
    }
    
    func hasIndex(index:Int) -> Bool {
        if(pictures.count > index) {
            return true
        } else {
            return false
        }
    }
    
    func getImageByIndex(index:Int) -> UIImage {
        let currentPicture = getPictureById(index)
        return currentPicture.getImageObject()
    }
    
    func getTotalPictures() -> Int {
        return pictures.count
    }
    
    func getPicturesUploaded() -> [PictureToAdd] {
        let picturesToUpload = pictures.filter({ (pic) -> Bool in
            if (pic.isPictureUploaded()) {
                return true
            } else {
                return false
            }
        })
        return picturesToUpload
    }
    
    func isAllPicturesUploaded() -> Bool {
        var pics = getPictureListToUpload()
        if(pics.count == 0) {
            return true
        } else {
            return false
        }
    }
    
    func getPicturesToUploadAndUploaded() -> [PictureToAdd] {
        let picturesToUpload = pictures.filter({ (pic) -> Bool in
            if (!pic.isDummyPicture()) {
                return true
            } else {
                return false
            }
        })
        return picturesToUpload
    }
    
    func getPicturesToUploadAndUploadedTotal() -> Int {
        let picturesToUpload = pictures.filter({ (pic) -> Bool in
            if (!pic.isDummyPicture()) {
                return true
            } else {
                return false
            }
        })
        return picturesToUpload.count
    }
    
    func deletePictureByIndex(index: Int) {
        pictures.removeAtIndex(index)
    }
    
    func getPictureListToUpload() -> [PictureToAdd] {
        return pictures.filter({ (pic) -> Bool in
            if (!pic.isPictureUploaded() && !pic.isDummyPicture()) {
                return true
            } else {
                return false
            }
        })
    }
    
    func setUploadedPicture(uniqueName: String) {
        let pictureIndexFound = searchPictureIndexByUniqueName(uniqueName)
        if(pictureIndexFound == INDEX_NOT_FOUND) {
            UploadPictureException(name: "searchPictureByUniqueNameNotFound", reason: "Thiss unique ID doesn't exist : \(uniqueName)").raise()
        }
        pictures[pictureIndexFound].pictureDidUpload()
        pictures[pictureIndexFound].updateUploadProgress(PROGRESS_DONE)
    }
    
    func searchPictureIndexByUniqueName(uniqueName: String) -> Int {
        var pictureIndex = INDEX_NOT_FOUND
        for (index, pic) in enumerate(pictures) {
            if(pic.getUniqueName() == uniqueName) {
                pictureIndex = index
            }
        }
        return pictureIndex
    }
    
    func updatePictureUploadProgressByIndex(index: Int, progress: Float) {
        pictures[index].updateUploadProgress(progress)
    }
    
    func getPictureUploadProgressByIndex(index: Int) -> Float {
        return pictures[index].getUploadProgress()
    }
    
}

var pictureListToAdd = PictureListToAdd()
